import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd());

  return {
    plugins: [vue()],
    // 配置前端服务地址和端口
    server: {
      // 服务器主机名，如果允许外部访问，可设置为"0.0.0.0"
      host: "0.0.0.0",
      port: Number(env.VITE_SERVER_PORT), //自定义端口
      proxy: {
        "/dev/": {
          target: env.VITE_SERVER_HOST,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/dev/, ""),
        },
        "/server": {
          target: "http://localhost:" + env.VITE_NODE_SERVER_PORT,
          changeOrigin: true,
        },
      },
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
      },
    },
  };
});
